import { reqGetUserInfo } from '@/api'

const state = {
  profile: null, //登录者信息  null未知 false未登录  obj已登录
}

const mutations = {
  setProfile(state, { profile }) {
    state.profile = profile
  },
}

const actions = {
  async getProfile({ commit }) {
    let profile = null
    try {
      let { code, permissions, roles, user } = await reqGetUserInfo()
      //  获取成功
      if (+code === 200) {
        profile = {
          permissions,
          roles,
          user,
        }
      } else {
        //  获取失败
        profile = false
      }
    } catch (_) {}
    commit('setProfile', { profile })
    return profile
  },
}

const user = {
  namespaced: true,
  state,
  mutations,
  actions,
}

export default user
